package com.feizhi.inventory.fragment.exportdetails.exportloss;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.feizhi.inventory.R;
import com.feizhi.inventory.database.TaskDetails;

import java.util.List;

public class ExportLossAdapter extends RecyclerView.Adapter <ExportLossAdapter.ViewHolder> {

    private List<TaskDetails> taskDetailsList;
    private Context context;
    Intent intent;
    private OnClickListener onClickListener;

    public ExportLossAdapter(List<TaskDetails> taskDetailsList, Context context) {
        this.taskDetailsList = taskDetailsList;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_inventory_details, parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {

//        if (taskDetailsList.get(position).getState().equals("已盘点")) {
//            notifyItemRemoved(position);
//        }
//        if (taskDetailsList.get(position).getState().equals("已盘点")) {
//
//            holder.itemView.setVisibility(View.GONE);
//
//        }
        holder.item_number.setText(taskDetailsList.get(position).getId());
        holder.name.setText(taskDetailsList.get(position).getName());
        holder.number_text.setText(taskDetailsList.get(position).getEpcID());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClickListener.OnItemClickListener(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return taskDetailsList.size();
    }

    public void setData(List<TaskDetails> taskDetailsList) {
        this.taskDetailsList = taskDetailsList;
        notifyDataSetChanged();
    }

//    public void setTime(List<TaskDetails> taskDetailsList) {
//        this.taskDetailsList = taskDetailsList;
//        notifyDataSetChanged();
//    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView number_text, name, item_number;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            name = itemView.findViewById(R.id.name);
            number_text = itemView.findViewById(R.id.number_text);
            item_number = itemView.findViewById(R.id.date);

        }
    }

    /**
     *  写一个接口，用来实现Item的点击事件
     * */
    public interface OnClickListener {

        void OnItemClickListener(int position);

    }

    public void setItemOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }


    public void removed(String name) {

        for (int i = 0; i < taskDetailsList.size(); i++) {

            if (taskDetailsList.get(i).getName().equals(name)) {
                taskDetailsList.remove(i);
                notifyItemRemoved(i);
                break;
            }

        }
    }
}
